/*
 * user_interface.c
 *
 *  Created on: Mar 12, 2013
 *      Author: Aaron Kaufman
 */
#include <string.h>
#include <stdio.h>
#include "cpu.h"
#include "ProcessScheduler.h"
#include "data.h"
#include "producer.h"
#include "consumer.h"
#include "calculator.h"
#include "sharedMemory.h"

#include <stdlib.h>
#include "timer.h"
#include "keyboardInput.h"

int main(){
		sharedMemory_t(mem);
	   tdata_t(data);
	    init_sharedMemory(&mem);
	   init_data(&data, &mem);

	   char text [30]= "bees";
	   char letter;
	   printf("Enter a valid scheduling policy (r = round robin, p = priority, l = lottery): ");
		fflush(stdout);
	   scanf("%s", &text);
	   printf("%s", &text);
		fflush(stdout);

	   ScheduleType type;
	   //scanf("%s", &text);
	   int flag = 0;
	   while (flag == 0){
		   flag = 1;
		   if (strcmp(text, "r") == 0){
			   type = ROUND_ROBIN;
		   }
		   else if (strcmp(text, "p") == 0){
			   type = PRIORITY;
		   }
		   else if (strcmp(text, "l") == 0){
			   type = LOTTERY;
		   }
		   else{
			   flag= 0;
			   printf("Enter a valid scheduling policy (r = round robin, p = priority, l = lottery): ");
				fflush(stdout);
			   scanf("%s", &text);
				fflush(stdout);
		   }
	   }

	   CPU(cpu);
	   createCPU(&cpu);
	   int priority = 1;
	   int (* function_pointer)(tdata_t *data);
		flag = 0;
		int has_keyboard_input = 0;
		int has_calculator = 0;
		int has_producer = 0;
		int has_consumer = 0;

	   while (flag == 0){
			   printf("\nPlease enter the next process type you want to include in the CPU (c = calculator, p = producer, o = consumer, i = io, d = done)");
				fflush(stdout);
			   scanf("%s", &text);
				fflush(stdout);
		   if (strcmp(text, "d") ==0){
			   break;
		   }
		   else if ((strcmp(text, "c") == 0)){//Create calculator
			   if (has_calculator == 1)
				   {
				   printf("Calculator already exists");
					fflush(stdout);
				   continue;
				   }
			   else
				   {
				   function_pointer = next_calc_instr;
				   has_calculator = 1;
				   printf("Calculator created");
					fflush(stdout);
				   }
		   }
		   else if ((strcmp(text, "p") == 0)){//Create producer
			   if (has_producer == 1)
				   {
				   printf("Producer already exists");
					fflush(stdout);
				   continue;
				   }
			   else
				   {
				   function_pointer = next_pro_instr;
				   has_producer = 1;
				   printf("Producer created");
					fflush(stdout);
				   }
		   }
		   else if ((strcmp(text, "i") == 0)){//Create io thread
			   if (has_keyboard_input == 1)
				   {
				   printf("Keyboard input already exists");
					fflush(stdout);
				   continue;
				   }
			   else
				   {
				   has_keyboard_input = 1;
				   printf("Keyboard input created");
					fflush(stdout);
					continue;
				   }
		   }
		   else if ((strcmp(text, "o") == 0)){//Create consumer thread
			   if (has_consumer == 1)
				   {
				   printf("Consumer already exists");
					fflush(stdout);
				   continue;
				   }
			   else
			   	   {
				   function_pointer = next_con_instr;
				   has_consumer = 1;
				   printf("Consumer created");
					fflush(stdout);
			   	   }
			   has_consumer = 1;
		   }
		   else{

			   continue;
		   }

		   if (type == PRIORITY){

			   printf("Please enter the desired priority for this process (1 to 10):");
				fflush(stdout);
			   scanf("%d", &priority);
				fflush(stdout);
		   }
		    addProcessToCPUProcessList(function_pointer, priority, &cpu, &mem, &data);
	   }

	   if (has_keyboard_input ==1){

		  create_keyboard_input_thread();

	   }

	   timer_init(&cpu);
	   runCPU(type, &cpu);

	   return 0;
}
